//
//  DepoistInfoVC.swift
//  d11
//
//  Created by peale on 2018/10/21.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit

class DepoistInfoVC: BXBaseVC,UITableViewDelegate,UITableViewDataSource {
    let  reuseIdentifierID : String = "DepoistInfoTopCell1"
    let  reuseIdentifierID2 : String = "DepoistInputInfoCell"
    let  reuseIdentifierID3 : String = "DepoistInfoAddressCell"
    let  reuseIdentifierID4 : String = "DepoistBackCell"
    let  reuseIdentifierID5 : String = "DepoistTopHintCell"
    let  reuseIdentifierID6 : String = "DepoistInputItemCell"
    let  reuseIdentifierID7 : String = "DepoistInfoDateCell"
    let  reuseIdentifierID8 : String = "DepoistInfoItemCell"
    let  reuseIdentifierID9 : String = "DepoistBtnCell"
    let  reuseIdentifierID10 : String = "DepoistInfoItem2Cell"
    
    var pendingAtm :String = "false" //是否有未提交人工智能提案
    var moneyCount:String? //充值的金额
    var realName :String?   //真实姓名
    var bankNo :String?    //银行卡号
    var bankName :String?    //银行名称
    var bankicon:String?    //银行图片
    var address :String?    //地址
    var bankCss:String? //银行标示
    var bcid : Int? //id
    var atmBank :[String:Any]?
    var nickname :String?   //存款人昵称
    var remak:String?   //存款人备注
//    var year : String? //年
//    var month : String? //月
//    var day : String? //日
    lazy var tableView:UITableView = {
        let table = UITableView(frame:CGRect(x: 0, y: 0, width: self.view.width, height:kScreenHeight-kTabBarBottom), style:UITableView.Style.plain)
        table.dataSource = self
        table.delegate = self
        //table.layoutMargins = UIEdgeInsets.zero
        table.separatorStyle = .none
        table.backgroundColor  = UIColor.white
        table.showsVerticalScrollIndicator = false
        table.tableFooterView = UIView.init()
        table.separatorStyle = .none
        table.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        table.estimatedRowHeight = 0
        table.estimatedSectionHeaderHeight = 0
        table.estimatedSectionFooterHeight = 0
        if #available(iOS 11.0, *) {
            table.contentInsetAdjustmentBehavior = .never
        } else {
            self.automaticallyAdjustsScrollViewInsets = false
        }
        return table
    }()
    // 如果需要拦截系统返回按钮就重写该方法返回 false
    @objc override func currentViewControllerShouldPop() -> Bool {
        if self.pendingAtm == "true" {
            self.cancelATMDeposit()
            return false
        }else{
            DispatchQueue.main.async {
                if self.navigationController?.viewControllers.count ?? 0 >= 2{
                    let vc =  self.navigationController?.viewControllers[1];
                    self.navigationController?.popToViewController(vc!, animated: true)
                }
            }
            return false
        }
       
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "存款"
        self.view.backgroundColor = UIColor.white
        self.view.addSubview(self.tableView)
    }
    func numberOfSections(in tableView: UITableView) -> Int {
            return 3
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
              return 5
        }
//        else if section == 1{
//            return 2
//        }
//        else if section == 2{
//            return 1
//        }
        else if section == 1{
            return 3
        }else if section == 2{
            return 1
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0{
            if indexPath.row == 4 {
               // return 104.5
               return 80
            }
            return  60
 //       }else if indexPath.section == 1{
//            if indexPath.row == 0{
//                return 30
//            }else
//            if indexPath.row == 0{
//                return 115
//            }
        }else if indexPath.section == 1{
            if indexPath.row == 0{
                return 30
            }
            return 60
        }else if indexPath.section == 2{
            return 80
        }
        return  0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
         return UITableViewHeaderFooterView.init()
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print("\(indexPath.section)====\(indexPath.row)")
        if indexPath.section == 0{
            if indexPath.row == 0 {
                var cell :DepoistInfoTopCell? = tableView.dequeueReusableCell(withIdentifier: reuseIdentifierID as String) as? DepoistInfoTopCell
                if cell == nil {
                    cell =  DepoistInfoTopCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifierID as String)
                }
                cell?.setData(dict: ["title":self.bankName ?? "","imgName":self.bankicon ?? ""])
                return cell!
            }else if indexPath.row == 4{ //地址
                var cell :DepoistInfoAddressCell? = tableView.dequeueReusableCell(withIdentifier: reuseIdentifierID3 as String) as? DepoistInfoAddressCell
                if cell == nil {
                    cell =  DepoistInfoAddressCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifierID3 as String)
                }
                
                cell?.setData(inputText: self.address ?? "")
                return cell!
            }else{
                var cell :DepoistInputInfoCell? = tableView.dequeueReusableCell(withIdentifier: reuseIdentifierID2 as String) as? DepoistInputInfoCell
                if cell == nil {
                    cell =  DepoistInputInfoCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifierID2 as String)
                }
                if indexPath.row == 1{  //真是姓名
                    cell?.setData(inputText: self.realName ?? "", row: indexPath.row)
                }else if indexPath.row == 2{ // 银行卡号
                    cell?.setData(inputText: self.bankNo ?? "", row: indexPath.row)
                }else if indexPath.row == 3{ //充值金额
                    cell?.setData(inputText: self.moneyCount ?? "", row: indexPath.row)
                }
                return cell!
            }
   //     }
    //    else if indexPath.section == 1{
//            if indexPath.row == 0{
//                var cell :DepoistTopHintCell? = tableView.dequeueReusableCell(withIdentifier: reuseIdentifierID5 as String) as? DepoistTopHintCell
//                if cell == nil {
//                    cell =  DepoistTopHintCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifierID5 as String)
//                }
//                cell?.setData(hint: "")
//                return cell!
//            }else
//            if indexPath.row == 0{
//                var cell :DepoistInputItemCell? = tableView.dequeueReusableCell(withIdentifier: reuseIdentifierID6 as String) as? DepoistInputItemCell
//                if cell == nil {
//                    cell =  DepoistInputItemCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifierID6 as String)
//                }
//                 cell?.setData(money: self.moneyCount, enabled: false)
//                return cell!
//            }
//        }else if indexPath.section == 2{
//            var cell :DepoistInfoDateCell? = tableView.dequeueReusableCell(withIdentifier: reuseIdentifierID7 as String) as? DepoistInfoDateCell
//            if cell == nil {
//                cell =  DepoistInfoDateCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifierID7 as String)
//            }
//           cell?.setData(year: self.year, moth: self.month, day: self.day, row: indexPath.section)
//            return cell!
        }else if indexPath.section == 1{
            if indexPath.row == 0{
                var cell :DepoistTopHintCell? = tableView.dequeueReusableCell(withIdentifier: reuseIdentifierID5 as String) as? DepoistTopHintCell
                if cell == nil {
                    cell =  DepoistTopHintCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifierID5 as String)
                }
                   cell?.setData(hint: "以下请在完成存款后填写")
                return cell!
            }else if indexPath.row == 1{
                var cell :DepoistInfoItemCell? = tableView.dequeueReusableCell(withIdentifier: reuseIdentifierID8 as String) as? DepoistInfoItemCell
                if cell == nil {
                    cell =  DepoistInfoItemCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifierID8 as String)
                }
                cell?.callChangeBlock({ (name) in
                    self.nickname = name
                })
                cell?.setData(inputText: self.nickname ?? "", row: indexPath.row)
                return cell!
            }else if indexPath.row == 2{
                var cell :DepoistInfoItem2Cell? = tableView.dequeueReusableCell(withIdentifier: reuseIdentifierID10 as String) as? DepoistInfoItem2Cell
                if cell == nil {
                    cell =  DepoistInfoItem2Cell(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifierID10 as String)
                }
                cell?.callChangeBlock({ (remak) in
                    self.remak = remak
                })
                 cell?.setData(inputText: self.remak ?? "", row: indexPath.row)
                return cell!
            }
        
        }else if indexPath.section == 2{
            var cell :DepoistBtnCell? = tableView.dequeueReusableCell(withIdentifier: reuseIdentifierID9 as String) as? DepoistBtnCell
            if cell == nil {
                cell =  DepoistBtnCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifierID9 as String)
            }
            cell?.callClickBlock {
                //提交
                self.submit()
            }
            return cell!
        }
       
      return UITableViewCell.init()
    }
    
    func submit() -> Void {
        if self.nickname == nil {
            UIHelper.alertSimpleMessage(about: "请输入付款人姓名!", withDescription: "", in: self)
            return
        }
        if self.remak == nil {
            UIHelper.alertSimpleMessage(about: "请输入存款备注!", withDescription: "", in: self)
            return
        }
        self.showLoadHUD(title: "提交中")
        NetWorkRequest(API.submitATMDeposit(url: "a65ec98", bcid: String(bcid!), amount: self.moneyCount ?? "0", deposituser: self.nickname!, transno: self.remak!), completion: { (responseData,statusCode,strData) -> (Void) in
             self.dismissLoadHUD()
            DispatchQueue.main.async {
                UIHelper.alertSimpleMessage2(about: "存款提交成功", withDescription: "", in: self)
//                if self.navigationController?.viewControllers.count ?? 0 >= 2{
//                    let vc =  self.navigationController?.viewControllers[1];
//                    self.navigationController?.popToViewController(vc!, animated: true)
//                }
            }
        }, failed: { (faildInfo,reCode,msg) -> (Void) in
             self.dismissLoadHUD()
            DispatchQueue.main.async {
                UIHelper.alertSimpleMessage(about: "\(faildInfo)", withDescription: "", in: self)
//                if self.navigationController?.viewControllers.count ?? 0 >= 2{
//                    let vc =  self.navigationController?.viewControllers[1];
//                    self.navigationController?.popToViewController(vc!, animated: true)
//                }
            }
        }) { () -> (Void) in
             self.dismissLoadHUD()
            DispatchQueue.main.async {
                UIHelper.alertSimpleMessage(about: "提交存款异常!", withDescription: "", in: self)
//                if self.navigationController?.viewControllers.count ?? 0 >= 2{
//                    let vc =  self.navigationController?.viewControllers[1];
//                    self.navigationController?.popToViewController(vc!, animated: true)
//                }
            }
        }
    }
    
    //取消人工智能存款提案
    func cancelATMDeposit() -> Void {
         self.showLoadHUD(title: "取消存款中...")
        NetWorkRequest(API.cancelATMDeposit(url: "33c21e7", bcid:  String(bcid!), amount: self.moneyCount ?? "0"), completion: { (responseData,statusCode,strData) -> (Void) in
            self.dismissLoadHUD()
            DispatchQueue.main.async {
                if self.navigationController?.viewControllers.count ?? 0 >= 2{
                    let vc =  self.navigationController?.viewControllers[1];
                    self.navigationController?.popToViewController(vc!, animated: true)
                }
            }
        }, failed: { (faildInfo,reCode,msg) -> (Void) in
            self.dismissLoadHUD()
            DispatchQueue.main.async {
                UIHelper.alertSimpleMessage(about: "\(faildInfo)", withDescription: "", in: self)
                if self.navigationController?.viewControllers.count ?? 0 >= 2{
                    let vc =  self.navigationController?.viewControllers[1];
                    self.navigationController?.popToViewController(vc!, animated: true)
                }
            }
        }) { () -> (Void) in
            self.dismissLoadHUD()
            DispatchQueue.main.async {
                UIHelper.alertSimpleMessage(about: "网络异常!", withDescription: "", in: self)
                if self.navigationController?.viewControllers.count ?? 0 >= 2{
                    let vc =  self.navigationController?.viewControllers[1];
                    self.navigationController?.popToViewController(vc!, animated: true)
                }
            }
        }
    }
}
